package com.leoman.controller.backend;

import com.leoman.common.DataTableFactory;
import com.leoman.controller.common.CommonController;
import com.leoman.entity.UserBuyRecord;
import com.leoman.entity.UserMoney;
import com.leoman.service.UserBuyRecordService;
import com.leoman.service.UserMoneyService;
import com.leoman.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 财务管理
 * Created by 涂奕恒 on 2016/12/12.
 */
@Controller
@RequestMapping(value = "backend/finance")
public class FinanceController extends CommonController {

    @Autowired
    private UserMoneyService userMoneyService;

    @Autowired
    private UserBuyRecordService userBuyRecordService;

    @RequestMapping("index")
    public String index() {
        return "backend/充值列表";
    }

    @RequestMapping("gain/index")
    public String gainIndex() {
        return "backend/提现列表";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response,
                     Integer type,
                     String nickName,
                     String account,
                     Integer userType,
                     Integer accountType,
                     String startTime,
                     String endTime,
                     Integer draw,
                     Integer start,
                     Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);
        Page<UserMoney> page = userMoneyService.pageByParams(type, nickName, account, userType, accountType, startTime, endTime, pageNum, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    /**
     * 确认打款
     *
     * @param userMoneyId 申请体现ID
     * @return
     */
    @RequestMapping("/confirmMoney")
    @ResponseBody
    public Integer confirmMoney(Integer userMoneyId) {
        try {
            return userMoneyService.confirmMoney(userMoneyId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 一键确认提现
     */
    @RequestMapping("/batchConfirm")
    @ResponseBody
    public Integer batchConfirm(String ids) {
        try {
            return userMoneyService.batchConfirm(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @RequestMapping("/buyList")
    public void buyList(HttpServletResponse response,
                        String nickName,
                        String num,
                        String startTime,
                        String endTime,
                        Integer draw,
                        Integer start,
                        Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);
        Page<UserBuyRecord> page = userBuyRecordService.pageByParams(nickName, num, startTime, endTime, pageNum, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }
}
